package com.text.zzj;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientThread implements Runnable {
	private Socket clientS;
	
	private String serverHost;
	private int serverPort;

	public ClientThread(String serverHost, int serverPort) {
		super();
		this.serverHost = serverHost;
		this.serverPort = serverPort;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			clientS = new Socket(serverHost, serverPort);
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(clientS.getOutputStream(), "utf-8"));
			String strO = "我还能怎么办";
			// byte[] bo = strO.getBytes();
			pw.println(strO);
			String strO2 = "原谅他";
			pw.println(strO2);
			pw.print("eof");
			pw.flush();
			pw.flush();
			System.out.println("客户端发送的消息：" + strO + "\n" + strO2);

			BufferedReader br = new BufferedReader(new InputStreamReader(clientS.getInputStream(), "utf-8"));
			// byte[] b = new byte[1024];
			// int len = -1;
			String strLine = null;
			while (true) {
				strLine = br.readLine();

				// 结束读取内容
				if (strLine.equals("eof") && strLine != null) {
					System.out.println("服务器收到消息" + strLine);

				} else {
					break;
				}
			}
			pw.close();
			br.close();
			clientS.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
